package com.qingzhuge.manager.service.impl;

import com.qingzhuge.manager.dao.SysRoleDao;
import com.qingzhuge.manager.entity.SysRole;
import com.qingzhuge.manager.service.ISysRoleService;
import com.qingzhuge.resource.entity.SysResource;
import com.qingzhuge.service.BasePkLongServiceImpl;
import com.qingzhuge.utils.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.qingzhuge.manager.service.impl.SysRoleServiceImpl.CACHE_NAME;


/**
 * @author : zero.xiao
 * @description : 角色
 * @date :2018/6/8 0008 下午 15:08
 * @modified :
 * @since : 1.0.0
 */
@Service
@CacheConfig(cacheNames = CACHE_NAME)
public class SysRoleServiceImpl extends BasePkLongServiceImpl<SysRoleDao, SysRole> implements ISysRoleService {
    public static final String CACHE_NAME = "sysRoleService";
    @Override
    public PageUtil<SysRole> query(SysRole entity) {
        PageUtil<SysRole> query = super.query(entity);
        for (SysRole role : query.getContent()) {
            List<SysResource> resources = role.getResources();
            resources.forEach(res -> {
                String permission = res.getPermissions();
                if (StringUtils.isBlank(role.getPermission())) {
                    role.setPermission(permission);
                } else {
                    role.setPermission(role.getPermission() + ";" + permission);
                }
            });
        }
        return query;
    }

    @Override
    public List<SysRole> findList() {
        return baseDao.findList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysRole update(SysRole role) {
        return super.save(role);
    }

    @Override
    public SysRole findByRoot() {
        return baseDao.findByRoot();
    }
}
